package com.xinzhi.controller;

import com.xinzhi.model.Result;
import com.xinzhi.model.User;
import com.xinzhi.service.IUserService;
import com.xinzhi.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class UserController {

    @Autowired
    private IUserService iUserService;

    @RequestMapping("/login")
    @ResponseBody
    public Result logincheck(User user) {
        User logincheck = iUserService.logincheck(user);
        if (logincheck.getAccount()== null&&logincheck.getPassword()==null) {
            return Result.error(404, "登录失败");
        } else {
            return Result.ok(logincheck);
        }

    }

    @RequestMapping("/register")
    @ResponseBody
    public Result register(User user) {
        user.setPassword(MD5Utils.encrypt(user.getPassword()));
        boolean repetition = iUserService.repetition(user.getPhone());
        int register = iUserService.register(user);
        if (repetition) {
            if (register > 0) {
                System.out.println("注册成功");
                return Result.ok();
            }
        }
        return Result.error(404, "注册失败");
    }
    @RequestMapping(value = "/repetition",method = RequestMethod.GET)
    public Result repetition(String phone){
        boolean repetition = iUserService.repetition(phone);
        if(repetition){
            return Result.ok("注册成功");
        }
        return Result.error(404,"已注册");
    }

}
